//
//  SecurityViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/4/21.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "SecurityViewController.h"
#import "CheckSecurityViewController.h"
#import "FindWordViewController.h"

@interface SecurityViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@end

@implementation SecurityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=MyString(@"账户与安全");
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, MainW, MainH-64) style:1];
    tableView.backgroundColor=Gray_Color;
    tableView.delegate = self;
    tableView.dataSource=self;
    self.tableView=tableView;
    tableView.separatorColor=SeparatorColor;
    tableView.separatorInset=UIEdgeInsetsZero;
    tableView.separatorStyle=0;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    // Do any additional setup after loading the view.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 6;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 49;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"celll"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"celll"];
    }
    cell.selectionStyle=0;
    cell.textLabel.textColor=TextColor3;
    cell.textLabel.font=FontAndStyle14;
    cell.detailTextLabel.textColor=TextColor6;
    cell.detailTextLabel.font=FontAndStyle14;
    if (indexPath.section==0) {
        cell.textLabel.text=MyString(@"昵称");
       cell.detailTextLabel.text=[ZJUserDefaultsTools getCurrentNickName];
    }else if (indexPath.section==1){
        cell.textLabel.text=MyString(@"修改手机号码");
        cell.detailTextLabel.text=[NSString stringWithFormat:@"%@",[ZJUserDefaultsTools getCurrentUserPhone]];
        cell.accessoryType=1;
    }else if (indexPath.section==2){
        cell.textLabel.text=MyString(@"修改登录密码");
        cell.accessoryType=1;
    }else if (indexPath.section==3){
        cell.textLabel.text=MyString(@"账号绑定");
        cell.backgroundColor=Gray_Color;
    }else if (indexPath.section==4){
        cell.textLabel.text=@"QQ";
        cell.imageView.image=[UIImage imageNamed:@"icon_safe_qq"];
        if ([ZJUserDefaultsTools isQQBind]==YES) {
            cell.detailTextLabel.text=MyString(@"已绑定");
            cell.detailTextLabel.textColor=TextColor6;
        }else{
            cell.detailTextLabel.text=MyString(@"未绑定");
            cell.detailTextLabel.textColor=Red_Color;
        }
        
    }else if (indexPath.section==5){
        cell.textLabel.text=@"微信";
        cell.imageView.image=[UIImage imageNamed:@"icon_safe_wechat"];
        if ([ZJUserDefaultsTools isWeChatBind]==YES) {
            cell.detailTextLabel.text=MyString(@"已绑定");
            cell.detailTextLabel.textColor=TextColor6;
        }else{
            cell.detailTextLabel.text=MyString(@"未绑定");
            cell.detailTextLabel.textColor=Red_Color;
        }

    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
    switch (indexPath.section) {
        case 1:
        {
            [self showToast:@"暂时不支持修改手机号"];
//            CheckSecurityViewController*vc=[[CheckSecurityViewController alloc]init];
//            vc.styple=1;
//            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:
        {
            
            CheckSecurityViewController*vc=[[CheckSecurityViewController alloc]init];
            vc.styple=2;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 4:
        {
            if ([ZJUserDefaultsTools isQQBind]==YES) {
                
                LXAlertView*alert=[[LXAlertView alloc]initWithTitle:nil message:MyString(@"确定要解除绑定吗?") cancelBtnTitle:MyString(@"取消") otherBtnTitle:MyString(@"确定") clickIndexBlock:^(NSInteger clickIndex) {
                    if (clickIndex==1) {
                       NSLog(@"解绑");
                        [self QQRemoveBind];
                    }
                }];
                [alert showLXAlertView];
            }else{
                NSLog(@"暂未绑定");
            }
        }
            break;
        case 5:
        {
            if ([ZJUserDefaultsTools isWeChatBind]==YES) {
                LXAlertView*alert=[[LXAlertView alloc]initWithTitle:nil message:MyString(@"确定要解除绑定吗?") cancelBtnTitle:MyString(@"取消") otherBtnTitle:MyString(@"确定") clickIndexBlock:^(NSInteger clickIndex) {
                    if (clickIndex==1) {
                        NSLog(@"解绑");
                        [self WeChatRemoveBind];
                    }
                }];
                [alert showLXAlertView];
            }else{
                NSLog(@"暂未绑定");
            }
        }
            break;

            
        default:
            break;
    }
}
-(void)QQRemoveBind{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    [self doHttpRequestWithApi:API_QQREMOVEBIND method:POST dic:dic successBlock:^(NSDictionary *dic) {
        if ([dic[@"status"]intValue]==0) {
            [MBProgressHUD showSuccess:MyString(@"解绑成功")];
            [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:QQBIND];
            [self.tableView reloadData];
        }else{
            [MBProgressHUD showError:MyString(@"解绑失败")];
        }
        
    }];
}
-(void)WeChatRemoveBind{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    [self doHttpRequestWithApi:API_WechatREMOVEBIND method:POST dic:dic successBlock:^(NSDictionary *dic) {
        if ([dic[@"status"]intValue]==0) {
            [MBProgressHUD showSuccess:MyString(@"解绑成功")];
             [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:WECHATBIND];
            [self.tableView reloadData];
        }else{
            [MBProgressHUD showError:MyString(@"解绑失败")];
        }

    }];
}
@end
